package ae.pcfc.cldos.online.common.clddb.dao.provider;

import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import ae.pcfc.cldos.online.common.clddb.dao.TempFileStorageDAO;
import ae.pcfc.cldos.online.common.clddb.domain.TempFileStorage;
import ae.pcfc.cldos.online.common.dao.GenericDAO;

/**
 * Test storage DAO implementation
 * 
 * @author arosa
 *
 */
public class TempFileStorageDAOImpl extends GenericDAO implements TempFileStorageDAO {

	public Long add(TempFileStorage tempFileStorage){
		saveOrUpdate(tempFileStorage);
		return tempFileStorage.getId();
	}
	
	public void remove(TempFileStorage tempFileStorage){
		delete(tempFileStorage);
	}
	
	public void update(TempFileStorage tempFileStorage){
		updateNotCascade(tempFileStorage);
	}
	
	public void delete(Long fileId){
		deleteById(TempFileStorage.class, fileId);
	}
	
	public void deleteTransferedFiles(){
		runNamedQuery(TempFileStorage.DELETE_TRANSFERED_FILES_NAMED_QUERY_NAME);
	}
	
	public TempFileStorage getById(Long fileId){
		return findById(TempFileStorage.class, fileId);
	}
	/**
	 * @param appId
	 */
	@SuppressWarnings("unchecked")
	public List<TempFileStorage> getPaymentVouchers(Long appId){
		DetachedCriteria criteria = DetachedCriteria.forClass(TempFileStorage.class);
		criteria.add(Restrictions.eq(TempFileStorage.__APPLICATION_ID, appId));
		criteria.add(Restrictions.eq(TempFileStorage.__ATTACHMENT_TYPE_CODE,TempFileStorage.__PAYMENT_VOUCHER_DOCUMENT_TYPE_CODE));
		return (List<TempFileStorage>)getListByCriteria(criteria);
	}
	
	public void deleteFileRecordsByIds(List<Long> ids){
		for (Iterator<Long> iterator = ids.iterator(); iterator.hasNext();) {
			Long id = (Long) iterator.next();
			deleteById(TempFileStorage.class, id);
		}
	}
}
